<template>
	<div class="app-container">
		<template v-if="flag">
			<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
				<el-form-item label="文章标题" prop="title">
					<el-input
						v-model="queryParams.title"
						placeholder="请输入文章标题"
						clearable
						size="small"
						@keyup.enter.native="handleQuery"
					/>
				</el-form-item>
				<el-form-item label="操作人员" prop="createBy">
					<el-input
						v-model="queryParams.createBy"
						placeholder="请输入操作人员"
						clearable
						size="small"
						@keyup.enter.native="handleQuery"
					/>
				</el-form-item>
				<el-form-item>
					<el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
					<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
				</el-form-item>
			</el-form>

			<el-row :gutter="10" class="mb8">
				<el-col :span="1.5">
					<el-button
						type="primary"
						icon="el-icon-plus"
						size="mini"
						@click="handleAdd"
					>新增
					</el-button>
				</el-col>
				<el-col :span="1.5">
					<el-button
						type="success"
						icon="el-icon-edit"
						size="mini"
						:disabled="single"
						@click="handleUpdate"
					>修改
					</el-button>
				</el-col>
				<el-col :span="1.5">
					<el-button
						type="danger"
						icon="el-icon-delete"
						size="mini"
						:disabled="multiple"
						@click="handleDelete"
					>删除
					</el-button>
				</el-col>
				<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
			</el-row>

			<el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="55" align="center"/>
				<el-table-column label="序号" align="center" prop="articleId" width="100"/>
				<el-table-column label="文章标题" align="center" :show-overflow-tooltip="true">
					<template slot-scope="scope">
						<el-button type="text" size="mini" @click="showContent(scope.row)">{{
								scope.row.title
							}}
						</el-button>
					</template>
				</el-table-column>
				<el-table-column
					label="状态"
					align="center"
					prop="status"
					width="100"
				>
					<template slot-scope="scope">
						{{ scope.row.status == 1 ? "草稿" : "发布" }}
					</template>
				</el-table-column>
				<el-table-column label="创建者" align="center" width="100">
					<template slot-scope="scope">
						{{ scope.row.doctor.sysUser.userName }}
					</template>
				</el-table-column>
				<el-table-column label="创建时间" align="center" prop="createTime" width="100">
					<template slot-scope="scope">
						<span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center">
					<template slot-scope="scope">
						<el-button
							size="mini"
							type="text"
							icon="el-icon-edit"
							@click="handleUpdate(scope.row)"
						>修改
						</el-button>
						<el-button
							size="mini"
							type="text"
							icon="el-icon-delete"
							@click="handleDelete(scope.row)"
						>删除
						</el-button>
					</template>
				</el-table-column>
				<el-table-column label="审核状态" align="center">
					<template slot-scope="scope">
						<el-button
							size="mini"
							type="text"
              :disabled=true
              v-if="scope.row.reviewStatus==1"
						>已通过
						</el-button>
						<el-button
							size="mini"
							type="text"
              :disabled=true
              v-if="scope.row.reviewStatus==3"
						>未通过
						</el-button>
						<el-button
							size="mini"
							type="text"
							@click="examineStatusUpdate(scope.row)"
              v-if="scope.row.reviewStatus==0&&scope.row.approvalStatus==1"
						>审核通过
						</el-button>
						<el-button
							size="mini"
							type="text"
              :disabled=true
              v-if="scope.row.approvalStatus==0"
						>等待专家审核
						</el-button>
					</template>
				</el-table-column>
			</el-table>

			<pagination
				v-show="total>0"
				:total="total"
				:page.sync="queryParams.pageNum"
				:limit.sync="queryParams.pageSize"
				@pagination="getList"
			/>

			<!-- 添加或修改文章对话框 -->
			<el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
				<el-form ref="form" :model="form" :rules="rules" label-width="80px">
					<el-row>
						<el-col :span="12">
							<el-form-item label="文章标题" prop="title">
								<el-input v-model="form.title" placeholder="请输入文章标题"/>
							</el-form-item>
						</el-col>
						<el-col :span="12">
						</el-col>
						<el-col :span="24">
							<el-form-item label="状态">
								<el-radio-group v-model="form.status">
									<el-radio :label="1">草稿</el-radio>
									<el-radio :label="0">发布</el-radio>
								</el-radio-group>
							</el-form-item>
							<el-form-item label="分类:" prop="dosageType">
								<template>
									<el-select size="small" v-model="form.classify" placeholder="请选择"
											   style="width: 92%;">
										<el-option
											v-for="item in articleClassification"
											:key="item.dictValue"
											:label="item.dictLabel"
											:value="item.dictSort">
										</el-option>
									</el-select>
								</template>
							</el-form-item>
							<el-form-item label="文章类型:" prop="category">
								<template>
									<el-input size="small" v-model=form.dept.deptName :disabled="true"
											  class="deptInput"></el-input>
									<el-button size="small" @click="treeType" class="deptBtn">+</el-button>
								</template>
							</el-form-item>
							<el-form-item label="疾病:" prop="category">
								<template>
									<el-input v-model=form.illness.illnessName :disabled="true"
											  class="deptInput"></el-input>
									<el-button size="small" @click="illTreeType" class="deptBtn">+</el-button>
								</template>
							</el-form-item>
						</el-col>
						<el-col :span="24">
							<el-form-item label="内容">
								<Editor v-model="form.content"/>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
				<div slot="footer" class="dialog-footer" style="padding-top:30px">
					<el-button type="primary" @click="submitForm">确 定</el-button>
					<el-button @click="cancel">取 消</el-button>
				</div>
			</el-dialog>
			<el-dialog
				title="提示"
				:visible.sync="dialogVisible"
				width="30%">
				<el-tree class="tree" :data="tableData" node-key="typeId"
						 :highlight-current="true" default-expand-all :expand-on-click-node="false"
						 @node-click="handleXmxz">
				<span class="custom-tree-node" slot-scope="{ node, data }">
					<span>
						<i icon-class="el-icon-folder"
						   v-show="!node.isLeaf && !node.expanded"></i>
              <i icon-class="el-icon-document"
				 v-show="!node.isLeaf && node.expanded"></i>
						<el-badge>{{ data.typeName }}</el-badge>
					</span>
				</span>
				</el-tree>
				<span slot="footer" class="dialog-footer">
					<el-button @click="dialogVisible = false">取 消</el-button>
					<el-button type="primary" @click="dialogVisible = false">确 定</el-button>
				</span>
			</el-dialog>
			<el-dialog
				title="提示"
				:visible.sync="dialogIll"
				width="30%">
				<el-tree
					:data="tableIllData"
					highlight-current:true
					check-on-click-node:true
					show-checkbox:true
					default-expand-all
					node-key="id"
					ref="tree"
					highlight-current
					@node-click="handleXmxzs"
					:props="defaultProps">
					<span slot-scope="{ data }" :title="data.label">
						<span v-if="data.children != null">
							<i class="el-icon-folder"></i>{{ data.label }}
						</span>
						<span v-else>
							<i class="el-icon-document"></i>{{ data.label }}
						</span>
					</span>
				</el-tree>
				<span slot="footer" class="dialog-footer">
					<el-button @click="dialogIll = false">取 消</el-button>
					<el-button type="primary" @click="dialogIll = false">确 定</el-button>
				</span>
			</el-dialog>
		</template>
		<template v-else>
			<el-page-header @back="goBack" content="详情页面">
			</el-page-header>
			<h1 style="text-align: center">{{ currentRow.title }}</h1>
			<div v-html="currentRow.content" style="padding: 20px 50px 50px 50px;overflow: hidden;"></div>
		</template>
	</div>
</template>

<script>
import {IllnessListTree} from "@/api/illness/illness";
import {listType} from '@/api/hospital/articleType'
import {GetArticleList, DeleteArticleList, InsertArticleList, EditArticleList} from "@/api/healthy/article";
import Editor from '@/components/Editor';
import {mapGetters} from 'vuex'

export default {
	components: {
		Editor
	},
  computed: {
    ...mapGetters([
      'name',
      'userid'
    ]),
  },
	data() {
		return {
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 文章表格数据
			noticeList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			//是否弹出文章类型树
			dialogVisible: false,
			//是否弹出疾病树
			dialogIll: false,
			//文章类型数据接收
			tableData: [],
			//疾病数据接收
			tableIllData: [],
			// 类型数据字典
			statusOptions: [],
			flag: true,
			currentRow: {},
			defaultProps: {
				children: 'children',
				label: 'label'
			},
			//文章分类 字典字段
			articleClassification: [],
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				title: undefined,
				createBy: undefined,
				status: undefined
			},
			typeId: '',
			deptName: '',
			// 表单参数
			form: {
				dept: {
					deptName: '',
					typeId: '',
				},
				illness: {
					illnessName: '',
				},
				title: '',
				status: ''
			},
			// 表单校验
			rules: {
				title: [
					{required: true, message: "文章标题不能为空", trigger: "blur"}
				],
				noticeType: [
					{required: true, message: "文章类型不能为空", trigger: "blur"}
				]
			}
		};
	},
	created() {
		this.getList();
		this.getDeptTypeList();
		this.getIllTypeList();
		this.getDicts("sys_notice_status").then(response => {
			this.statusOptions = response.data;
		});
	},
	filters: {
		articleClassificationFilter: function (text) {
			console.log(text)
			// if (text != null) {
			//   for (let i = 0; i < this.articleClassification.length; i++) {
			//     if (this.articleClassification[i].dictValue === text.toString()) {
			//       return this.articleClassification[i].dictLabel;
			//     }
			//   }
			// }
		},
	},
	methods: {
		/** 查询文章列表 */
		getList() {
			this.loading = true;
			GetArticleList(this.queryParams).then(response => {
				this.noticeList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 文章状态字典翻译
		statusFormat(row, column) {
			return this.selectDictLabel(this.statusOptions, row.status);
		},
		// 文章状态字典翻译
		// typeFormat(row, column) {
		//   return this.selectDictLabel(this.typeOptions, row.noticeType);
		// },
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
    examineStatusUpdate(row){
      let articleId = row.articleId || this.ids
      this.$confirm('是否确认审核通过文章编号为"' + articleId + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        EditArticleList({articleId:articleId,reviewStatus:1,isTop:0,reviewUser:this.name}).then(response => {
          if (response.code === 200) {
            this.msgSuccess("审核成功");
            this.getList();
          }
        });
      })
    },
		// 表单重置
		reset() {
			this.form = {
				dept: {deptName: ''},
				illness: {illnessName: ''},
				articleId: undefined,
				title: undefined,
				noticeType: undefined,
				content: undefined,
				status: 0
			};
			this.resetForm("form");
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.noticeId)
			this.single = selection.length != 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = "添加文章";
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			let articleId = row.articleId || this.ids
			GetArticleList({'articleId': articleId}).then(res => {
				if (res.code === 200) {
					this.reset();
					this.open = true;
					this.title = "修改文章";
					this.form = res.rows[0];
				}
			});
		},
		/** 提交按钮 */
		submitForm: function () {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.articleId != undefined) {
						EditArticleList(this.form).then(response => {
							if (response.code === 200) {
								this.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							}
						});
					} else {
						InsertArticleList(this.form).then(response => {
							if (response.code === 200) {
								this.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							}
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			let articleId = row.articleId || this.ids
			this.$confirm('是否确认删除文章编号为"' + articleId + '"的数据项?', "警告", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(function () {
				return DeleteArticleList(articleId);
			}).then(() => {
				this.getList();
				this.msgSuccess("删除成功");
			}).catch(function () {
			});
		},
		/** 文章类型树 */
		treeType() {
			this.dialogVisible = true;
		},
		/** 疾病树 */
		illTreeType() {
			this.dialogIll = true;
		},
		getDeptTypeList() {
			listType().then(res => {
				this.tableData = this.handleTree(res.data, "typeId");
			})
			// GetDeptTypeList(this.params).then(res => {
			//   this.tableData = res.data;
			// });
			this.getDicts("article_classify").then(response => {
				this.articleClassification = response.data;
				console.log(this.articleClassification)
			});
		},
		getIllTypeList() {
			IllnessListTree(this.params).then(res => {
				this.tableIllData = res.data;
			});
		},
		//文章类型选择
		handleXmxz(obj, node, data) {
			console.log(obj)
			if (obj.children == null) {
				if (this.typeId == '') {
					this.typeId = obj.typeId;
					this.form.typeId = this.typeId
					this.deptName = obj.typeName;
					this.form.dept.deptName = this.deptName
				} else {
					this.typeId = this.form.typeId + ',' + obj.typeId;
					this.form.typeId = this.typeId
					this.deptName = this.form.dept.deptName + ',' + obj.typeName;
					this.form.dept.deptName = this.deptName
				}
			}
			console.log(this.form.typeId)
		},
		handleXmxzs(obj, node, data) {
			if (obj.children == null) {
				this.form.illnessId = obj.id;
			}
			this.form.illness.illnessName = obj.label
		},
		showContent(row) {
			this.currentRow = row;
			this.flag = !this.flag;
		},
		goBack() {
			this.flag = !this.flag;
		}
	}
};
</script>
<style lang="scss" scoped>
.deptInput, .deptBtn {
	float: left;
}

.deptInput {
	width: 200px;
}

.deptBtn {
	margin-left: 10px;
}
</style>
